package com.sdry.web.controller.llm;

import java.util.HashMap;
import java.util.List;
import java.util.Map;

import javax.annotation.Resource;
import javax.servlet.http.HttpServletResponse;

import org.springframework.stereotype.Controller;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RequestParam;

import com.sdry.model.hpf.Page;
import com.sdry.model.llm.EmployeeRecord;
import com.sdry.service.llm.EmployeeRecordService;
import com.sdry.utils.ResponseUtil;

import net.sf.json.JSONArray;
import net.sf.json.JSONObject;
import net.sf.json.JsonConfig;

/**
 * 员工工作记录的controller
 * <p>Title: EmployeeRecordController</p>
 * @author llm
 * @date 2018年11月21日
 * @version 1.0
 */
@Controller
@RequestMapping("employeeRecordMana")
public class EmployeeRecordController {

	/**
	 * 注入员工管理service
	 */
	@Resource
	private EmployeeRecordService employeeRecordService;
	
	/**
	 * 分页查询员工工作记录信息
	 * @param page
	 * @param teamRecord
	 * @param response
	 * @throws Exception
	 */
	@RequestMapping(value="/listPageEmployeeRecord")
	public void listPageEmployeeRecord(Page page, EmployeeRecord employeeRecord, HttpServletResponse response) throws Exception{
		//分页条件集合（页数，步长，查询条件）
		Map<String, Object> map = new HashMap<>();
		map.put("limit", page.getLimit());
		map.put("page", page.getPage());
		if (null != employeeRecord.getUserName()) {
			map.put("userName", employeeRecord.getUserName());
		} else {
			map.put("userName", "");
			employeeRecord.setUserName("");
		}
		if(null != employeeRecord.getTid()){
			map.put("tid", employeeRecord.getTid());
		}
		//获取当天班组中员工的记录的数量
		int count = employeeRecordService.countEmployeeRecordByTid(employeeRecord);
		//分页查询当天班组中员工的记录
		List<EmployeeRecord> list = employeeRecordService.listPageEmployeeRecord(map);
		//把数据存为JSON格式传到页面
		JSONObject jsonObject = new JSONObject();
		JsonConfig jsonConfig = new JsonConfig();
		JSONArray jsonArray = JSONArray.fromObject(list, jsonConfig);
		jsonObject.put("code", 0);
		jsonObject.put("data", jsonArray);
		jsonObject.put("count", count);
		ResponseUtil.write(response, jsonObject);
	}
	
	/**
	 * 删除员工记录
	 * @param id 员工记录ID
	 * @param response
	 * @throws Exception 
	 */
	@RequestMapping(value="/deleteEmployeeRecord")
	public void deleteTeamRecord(@RequestParam("id") Long id, HttpServletResponse response) throws Exception{
		int isSuccess = employeeRecordService.deleteEmployeeRecord(id);
		ResponseUtil.write(response, isSuccess);
	}
}
